package com.example.demo.controller;

import com.example.demo.annotation.PrintLog;
import com.example.demo.config.FileParms;
import com.example.demo.po.User;
import com.example.demo.service.UserService;
import com.example.demo.serviceimpl.TestImpl;
import com.vdurmont.emoji.EmojiParser;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.SocketUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Api(value = "测试管理", tags = {"测试"})
@RestController
@RequestMapping("/index")
@Slf4j
public class TestController {

    @Autowired
    private UserService userService;
    @Autowired
    private FileParms fileParms;

    @Autowired
    private TestImpl testimpl;

    @GetMapping("/say")
    public String sayHello(){
        System.out.println("test.................");
        log.info("info.........");
        return "test";
    }

    @PostMapping("/save")
    @PrintLog(value = "添加用户")
    public void save(@RequestBody User user){
        if(user == null )
            throw new RuntimeException("参数不能为空!");
        if(StringUtils.isEmpty(user.getUserName())){
            throw new RuntimeException("yhm不能为空!");
        }
        if(StringUtils.isEmpty(user.getUserNumber())){
            throw new RuntimeException("用户编号不能为空!");
        }
        try {
            String address = user.getAddress();
            System.out.println("address="+address);
            String s = EmojiParser.parseToAliases(address);
            user.setAddress(s);
            userService.save(user);
        }catch (Exception e){
            throw new RuntimeException("添加用户失败！");
        }
    }

    @GetMapping("/findByUserNumber")
    @PrintLog("根据用户编号查询")
    public Object findUserByUserNumber(@RequestParam String usernumber){
        User user = userService.findUserByUserNumber(usernumber);
        String title = user.getAddress();
        String s = EmojiParser.parseToUnicode(title);
        System.out.println("s=="+s);
        user.setAddress(s);
        return user;
    }

    @PostMapping("/updateUserInfo")
    @PrintLog("修改用户信息")
    public void updateUserInfo(@RequestBody User user){
        userService.updateUserInfo(user);
    }


    @PostMapping("/dealFiles")
    public void dealFiles(){
        testimpl.showData();
        log.info(fileParms.toString());
    }

    @RequestMapping("html/get")
    public ModelAndView index(){
        System.out.println("html >>>>>");
        return new ModelAndView("index");
    }
}
